home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Edit List and Spin / NotepadCloneWithEdit / FindReplaceDialog.cs next >
Encoding:
Text File  |  2001-01-15  |  6.9 KB  |  198 lines

  1. //------------------------------------------------
  2. // FindReplaceDialog.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class FindDialog: FindReplaceDialog
  9. {
  10.      public FindDialog()
  11.      {
  12.           Text = "Find";
  13.  
  14.           labelReplace.Visible = false;
  15.           txtboxReplace.Visible = false;
  16.           btnReplace.Visible = false;
  17.           btnReplaceAll.Visible = false;
  18.           btnCancel.Location = btnReplace.Location;
  19.      }
  20. }
  21. class ReplaceDialog: FindReplaceDialog
  22. {
  23.      public ReplaceDialog()
  24.      {
  25.           Text = "Replace";
  26.  
  27.           grpboxDirection.Visible = false;
  28.      }
  29. }
  30. abstract class FindReplaceDialog: Form
  31. {
  32.                                                        // Fields
  33.      protected Label       labelFind, labelReplace;
  34.      protected TextBox     txtboxFind, txtboxReplace;
  35.      protected CheckBox    chkboxMatchCase;
  36.      protected GroupBox    grpboxDirection;
  37.      protected RadioButton radiobtnUp, radiobtnDown;
  38.      protected Button      btnFindNext, btnReplace, btnReplaceAll, btnCancel;
  39.  
  40.                                                        // Public events
  41.      public event EventHandler FindNext;
  42.      public event EventHandler Replace;
  43.      public event EventHandler ReplaceAll;
  44.      public event EventHandler CloseDlg;
  45.                                                        // Constructor
  46.      public FindReplaceDialog()
  47.      {
  48.           FormBorderStyle = FormBorderStyle.FixedDialog;
  49.           ControlBox      = false;
  50.           MinimizeBox     = false;
  51.           MaximizeBox     = false;
  52.           ShowInTaskbar   = false;
  53.           StartPosition   = FormStartPosition.Manual;
  54.           Location        = ActiveForm.Location +
  55.                             SystemInformation.CaptionButtonSize +
  56.                             SystemInformation.FrameBorderSize;
  57.  
  58.           labelFind = new Label();
  59.           labelFind.Parent = this;
  60.           labelFind.Text = "Fi&nd what:";
  61.           labelFind.Location = new Point(8, 8);
  62.           labelFind.Size = new Size(64, 8);
  63.  
  64.           txtboxFind = new TextBox();
  65.           txtboxFind.Parent = this;
  66.           txtboxFind.Location = new Point(72, 8);
  67.           txtboxFind.Size = new Size(136, 8);
  68.           txtboxFind.TextChanged += 
  69.                          new EventHandler(TextBoxFindOnTextChanged);
  70.  
  71.           labelReplace = new Label();
  72.           labelReplace.Parent = this;
  73.           labelReplace.Text = "Re&place with:";
  74.           labelReplace.Location = new Point(8, 24);
  75.           labelReplace.Size = new Size(64, 8);
  76.  
  77.           txtboxReplace = new TextBox();
  78.           txtboxReplace.Parent = this;
  79.           txtboxReplace.Location = new Point(72, 24);
  80.           txtboxReplace.Size = new Size(136, 8);
  81.           
  82.           chkboxMatchCase = new CheckBox();
  83.           chkboxMatchCase.Parent = this;
  84.           chkboxMatchCase.Text = "Match &case";
  85.           chkboxMatchCase.Location = new Point(8, 50); // 48);
  86.           chkboxMatchCase.Size = new Size(64, 8);
  87.  
  88.           grpboxDirection = new GroupBox();
  89.           grpboxDirection.Parent = this;
  90.           grpboxDirection.Text = "Direction";
  91.           grpboxDirection.Location = new Point(100, 40);
  92.           grpboxDirection.Size = new Size(96, 24);
  93.  
  94.           radiobtnUp = new RadioButton();
  95.           radiobtnUp.Parent = grpboxDirection;
  96.           radiobtnUp.Text = "&Up";
  97.           radiobtnUp.Location = new Point(8, 8);
  98.           radiobtnUp.Size = new Size(32, 12);
  99.  
  100.           radiobtnDown = new RadioButton();
  101.           radiobtnDown.Parent = grpboxDirection;
  102.           radiobtnDown.Text = "&Down";
  103.           radiobtnDown.Location = new Point(40, 8);
  104.           radiobtnDown.Size = new Size(40, 12);
  105.  
  106.           btnFindNext = new Button();
  107.           btnFindNext.Parent   = this;
  108.           btnFindNext.Text     = "&Find Next";
  109.           btnFindNext.Enabled  = false;
  110.           btnFindNext.Location = new Point(216, 4);
  111.           btnFindNext.Size     = new Size(64, 16);
  112.           btnFindNext.Click   += new EventHandler(ButtonFindNextOnClick);
  113.  
  114.           btnReplace = new Button();
  115.           btnReplace.Parent   = this;
  116.           btnReplace.Text     = "&Replace";
  117.           btnReplace.Enabled  = false;
  118.           btnReplace.Location = new Point(216, 24);
  119.           btnReplace.Size     = new Size(64, 16);
  120.           btnReplace.Click   += new EventHandler(ButtonReplaceOnClick);
  121.           
  122.           btnReplaceAll = new Button();
  123.           btnReplaceAll.Parent   = this;
  124.           btnReplaceAll.Text     = "Replace &All";
  125.           btnReplaceAll.Enabled  = false;
  126.           btnReplaceAll.Location = new Point(216, 44);
  127.           btnReplaceAll.Size     = new Size(64, 16);
  128.           btnReplaceAll.Click   += new EventHandler(ButtonReplaceAllOnClick);
  129.  
  130.           btnCancel = new Button();
  131.           btnCancel.Parent   = this;
  132.           btnCancel.Text     = "Cancel";
  133.           btnCancel.Location = new Point(216, 64);
  134.           btnCancel.Size     = new Size(64, 16);
  135.           btnCancel.Click   += new EventHandler(ButtonCancelOnClick);
  136.           CancelButton = btnCancel;
  137.  
  138.           ClientSize = new Size(288, 84);
  139.           AutoScaleBaseSize = new Size(4, 8);
  140.      }
  141.                                                        // Properties
  142.      public string FindText
  143.      {
  144.           set { txtboxFind.Text = value; }
  145.           get { return txtboxFind.Text; }
  146.      }
  147.      public string ReplaceText
  148.      {
  149.           set { txtboxReplace.Text = value; }
  150.           get { return txtboxReplace.Text; }
  151.      }
  152.      public bool MatchCase
  153.      {
  154.           set { chkboxMatchCase.Checked = value; }
  155.           get { return chkboxMatchCase.Checked; }
  156.      }
  157.      public bool FindDown
  158.      {
  159.           set 
  160.           { 
  161.                if (value)
  162.                     radiobtnUp.Checked = true;
  163.                else
  164.                     radiobtnDown.Checked = true;
  165.           }
  166.           get { return radiobtnDown.Checked; }
  167.      }
  168.                                                        // Event handlers
  169.      void TextBoxFindOnTextChanged(object obj, EventArgs ea)
  170.      {
  171.           btnFindNext.Enabled =
  172.           btnReplace.Enabled =
  173.           btnReplaceAll.Enabled = txtboxFind.Text.Length > 0;
  174.      }
  175.      void ButtonFindNextOnClick(object obj, EventArgs ea)
  176.      {
  177.           if (FindNext != null)
  178.                FindNext(this, EventArgs.Empty);
  179.      }
  180.      void ButtonReplaceOnClick(object obj, EventArgs ea)
  181.      {
  182.           if (Replace != null)
  183.                Replace(this, EventArgs.Empty);
  184.      }
  185.      void ButtonReplaceAllOnClick(object obj, EventArgs ea)
  186.      {
  187.           if (ReplaceAll != null)
  188.                ReplaceAll(this, EventArgs.Empty);
  189.      }
  190.      void ButtonCancelOnClick(object obj, EventArgs ea)
  191.      {
  192.           if (CloseDlg != null)
  193.                CloseDlg(this, EventArgs.Empty);
  194.  
  195.           Close();
  196.      }
  197. }
  198.